package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.page.PageData;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.ScientificMapper;
import com.bonc.ioc.model.Knowledges;
import com.bonc.ioc.service.ScientificService;
import com.bonc.ioc.util.SizeConversionUtils;
import com.bonc.ioc.vo.DirectoryVo;
import com.bonc.ioc.vo.KnowledgesVo;
import com.sun.tracing.dtrace.Attributes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @author liuyang
 * @creats 2021-04-08-18:04
 */
@Service
@Slf4j
public class ScientificServiceImpl extends BaseService implements ScientificService
{
    private static final Pattern PATTERN = Pattern.compile("[0-9]*");
    @Autowired
    KnowledgesEsServiceImpl knowledgesEsService;
    @Resource
    ScientificMapper scientificMapper;
    @Override
    public PageResult selectStandardInfoByPage(HashMap map) throws BindException {
        HashMap<String, Object> pageParams = this.getPageParams();
        map.putAll(pageParams);
        String orderBy = String.valueOf(map.get("orderBy"));
        if(StringUtils.isEmpty(orderBy)){
            map.remove("orderBy");
        }
        List<KnowledgesVo> list = scientificMapper.selectByPage(map);
        if(!CollectionUtils.isEmpty(list)){
            for(KnowledgesVo knowledgesVo : list){
                knowledgesVo.setFileSize(SizeConversionUtils.getSize(knowledgesVo.getFileSize()));
            }
        }
        PageResult pageResult = new PageResult(map,list);
        return pageResult;
    }

    @Override
    public KnowledgesVo selectByScientificId(Long id) {
        KnowledgesVo knowledgesVo = scientificMapper.selectByScientificId(id);
        knowledgesVo.setFileSize(SizeConversionUtils.getSize(knowledgesVo.getFileSize()));
        return knowledgesVo;
    }

    @Override
    public int updateDownloadsNum(Long id) {
        return scientificMapper.updateDownloadsNum(id);
    }

    @Override
    public int updateVisitNum(Long id) {
        return scientificMapper.updateVisitNum(id);
    }

    @Override
    public Map<String, Object> selectTitleBykeys(Integer id ,String keys,String title) {
        List<Knowledges> knowledges = knowledgesEsService.queryKnowledgesAssociation(keys);
        Map<String,Object> map = new HashMap<>(16);
        if(!CollectionUtils.isEmpty(knowledges)){
            Map<String,Object> iktMap = new HashMap<>(16);
            iktMap.put("id",id);
            iktMap.put("keys",keys);
            iktMap.put("title",title);
            map.put("keys",iktMap);
            map.put("knowledges",knowledges);
        }
        return map;
    }

    @Override
    public List<KnowledgesVo> selectByCreateTime(String pageSize, String pageNum) {
        if (StringUtils.isEmpty(pageSize) || !PATTERN.matcher(pageSize).matches()) {
            pageSize = "5";
        }
        if (StringUtils.isEmpty(pageNum) || !PATTERN.matcher(pageNum).matches()) {
            pageNum = "0";
        }
        Integer ps = Integer.parseInt(pageSize);
        Integer pn = Integer.parseInt(pageNum);
        return scientificMapper.selectByCreateTime(ps,pn);
    }

    @Override
    public List<DirectoryVo> getScientificDirs(String value) {
        return scientificMapper.getScientificDirs(value);
    }
}
